/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.<br/>
 * 项目名称 : 中科软支付平台
 * 创建日期 : 2016年12月13日
 * 修改历史 : 
 *     1. [2016年12月13日]创建文件 by liuyujia
 */
package com.sinosoft.sinopay.web.query.biz.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinosoft.sinocloud.platform.base.SinoBaseService;
import com.sinosoft.sinocloud.platform.common.exception.biz.IllegalRequestException;
import com.sinosoft.sinopay.merchants.facade.MerchantInfoFacadeService;
import com.sinosoft.sinopay.paytrade.facade.PaymentTradeQueryFacadeService;
import com.sinosoft.sinopay.web.query.biz.service.SingleRecordQueryService;
import com.sinosoft.sinopay.web.query.dto.PayInfoQryRequestDTO;
import com.sinosoft.sinopay.web.query.util.MD5SignUtil;

/**  
 * 【交易查询服务接口 实现】
 * @author dengcanyang
 * @date 2017年1月7日 下午20:42:04 
 * @version V1.0  
 */
@Service
public class SingleRecordQueryServiceImpl extends SinoBaseService implements SingleRecordQueryService{
	 @Autowired
	 private MerchantInfoFacadeService merchantSecurityFacadeService;
	 @Autowired
	 private PaymentTradeQueryFacadeService paymentTradeQueryFacadeService;
	 
	/** 商户签名信息缓存map */
	 private Map<String, String> signKeysMap;
	 
	@Override
	public void checkMerchantSignValid(String merchantCode, PayInfoQryRequestDTO tPayInfoQryRequestDTO, String signMsg) {
		if(signKeysMap==null || signKeysMap.size()==0){
			 //获取全部的商户签名信息
			 signKeysMap = merchantSecurityFacadeService.getAllMerchantSignKeys();
		 }
		//获取商户对应的key值
		signKeysMap = merchantSecurityFacadeService.getAllMerchantSignKeys();
		if(signKeysMap.get(merchantCode)==null){
			throw new IllegalRequestException("商户不存在");
		}
		String tSignKey = signKeysMap.get(merchantCode);
		//封装签名参数
		 Map<String,String> tSignInfoMap = new HashMap<String,String>();
		 tSignInfoMap.put("merchantCode",merchantCode);
		 tSignInfoMap.put("serialNo", tPayInfoQryRequestDTO.getSerialNo());
         tSignInfoMap.put("bizNo", tPayInfoQryRequestDTO.getBizNo());
         tSignInfoMap.put("bizType", tPayInfoQryRequestDTO.getBizType());
         tSignInfoMap.put("payNo", tPayInfoQryRequestDTO.getPayNo());
         tSignInfoMap.put("payType", tPayInfoQryRequestDTO.getPayType());
         if(tPayInfoQryRequestDTO.getPayNo()==null || tPayInfoQryRequestDTO.getPayNo().equals(0)){
        	 throw new IllegalRequestException("唯一编码 不能为空");
         }
        String data = "";
        try {
            data = MD5SignUtil.encryMD5(tSignInfoMap,tSignKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(signMsg ==null || !signMsg.equals(data)){
            throw new IllegalRequestException("签名不合法");
        }
	}

}
